package jmu.cze.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SessionInterceptor implements HandlerInterceptor {
    // 在拦截点执行前拦截
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();

        if(session.getAttribute("id")!=null) {
            String url = request.getRequestURI();
            System.out.println("url:"+url);
            if(url.contains("/admin") &&((Integer) session.getAttribute("type") != 1)) {
                response.sendRedirect(request.getContextPath()+"/user/login");
                return false;
            }
            return true;
        }else {
            response.sendRedirect(request.getContextPath()+"/user/login");
            return false;
        }
    }
}